package com.github.wj.security.server.configration;
import com.github.wj.common.security.util.DateFormatUtils;
import com.github.wj.security.server.component.HttpConfig;
import com.github.wj.security.server.interceptor.ErrorInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.servlet.MultipartConfigElement;
import java.text.ParseException;
import java.util.Date;

/**
 * web过滤器，拦截器配置
 * Created by 王建 on 2017/6/6.
 */
@Configuration
public class WebViewConfiguration extends WebMvcConfigurerAdapter {

    @Autowired
    private HttpConfig config;

    /**
     * 配置静态地址（不执行任何逻辑操作）
     *
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login").setViewName("login/index");
        registry.addViewController("/error").setViewName("common/error");
        registry.addViewController("/").setViewName("index/index");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/static/");
        //registry.addResourceHandler("/files/**").addResourceLocations("file:///var/spring/uploaded_files");
        //特别注意  在win环境下 路径最后一定要加“/”
        registry.addResourceHandler("/static/upload/**").addResourceLocations("file:///" + config.getFilePath());
        registry.addResourceHandler("/static/technological/**").addResourceLocations("file:///"+config.getImagePath());
        super.addResourceHandlers(registry);
    }

    /**
     * 错误页面过滤器
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new ErrorInterceptor()).addPathPatterns("/**");
        super.addInterceptors(registry);
    }

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //// 设置文件大小限制 ,超了，页面会抛出异常信息，这时候就需要进行异常信息的处理了;
        factory.setMaxFileSize("128MB"); //KB,MB
        /// 设置总上传数据总大小
        factory.setMaxRequestSize("256MB");
        //Sets the directory location where files will be stored.
        //factory.setLocation("路径地址");
        return factory.createMultipartConfig();
    }

    /**
     * spring 容器在启动的时候会把映射转化注册到容器里面。
     * 随着容器的启动而生效。
     * 有时候会缺少我们所需要的映射这样的话我们就需要自己给容器添加一个bean 来完成我们自己的映射
     *
     * @return
     */
    @Bean
    public Converter<String, Date> addNewConvert() {
        return new Converter<String, Date>() {
            @Override
            public Date convert(String source) {
                Date date = null;
                try {
                    date = DateFormatUtils.parse(source);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return date;
            }
        };
    }

}
